var util = require('../../utils/util.js');
var app = getApp();

Page({
  data: {
    screen_w: 320,
    screen_h: 480,
    showVideo: false,
    launchImageUrl: '',
    time: 5,
  },

  isFromShare: false,
  isFromSubMsg: false,
  fromSubUid: '',
  goLiaota: false,

  requestTimer: '',

  onLoad: function (data) {
    console.log("onLoad params:", data)
    
    this.setData({
      isIphone_x: app.isIphone_x,
      screen_w: app.screen_w,
      screen_h: app.screen_h
    });
    this.runOnLoad = true
    
    //点击或扫码分享进来的
    this.isFromShare = (data.share && data.share == 'true') ? true : false
    this.goLiaota = (data.goliaota && data.goliaota == 'true') ? true : false
    //点击订阅消息进来的
    this.isFromSubMsg = (data.fmsg && data.fmsg == 'true') ? true : false
    if (this.isFromShare) {
      this.fromSubUid = data.uid
    } else if (this.isFromSubMsg) {
      this.fromSubUid = data.fuid
    }

    app.getUserInfo(util.orgid, function (res) {
      console.log("小程序信息：", res)
      tt.setNavigationBarTitle({
        title: res.uname,
      })
    })

    this.getLaunchImage()
  },

      //index 001  获取默认分享图片
      getLaunchImage: function () {
        var that = this
        var params = {
          "publickey": util.publiccode,
          "ac": "indexslide",
          "uid": util.orgid,
          "block": '2'
        };
        util.api({
          data: params,
          success: function (responseData) {
            console.log("launchImageUrl info:", responseData);
            var result = responseData.data;
            if (result && result.length > 0) {
              that.setData({
                launchImageUrl: result[0].photo
              })
            }
          }
        });
      },  
 
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    var that = this
    app.initScreenInfo(function(res){
      if(res) {
        that.setData({
          isIphone_x: app.isIphone_x,
          screen_w: app.screen_w,
          screen_h: app.screen_h
        });
      }
    })
    
    let time = (new Date().getTime())/1000
    let gdTime = tt.getStorageSync('gdtime')
    console.log("nowtime = " + time + "; gdTime = " + gdTime)
    if (gdTime && gdTime > 0 && (time - gdTime) < 1200) {
      this.goAtherPageRedirect()
    } else {
      app.getWxCheck(function(show) {
        that.setData({
          showVideo: show
        })
        if(!show) {
          that.goAtherPageRedirect()
        } else {
          that.startTimer()
        }
      }, true)
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {
    this.clearTimer()
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {
    this.clearTimer()
  },

  /**开启定时器 */
  startTimer() {
    if(!this.data.showVideo) return
    this.clearTimer()
    var that = this
    console.log("开启定时器")
    that.requestTimer = setInterval (
      function () {
        let t = that.data.time
        t = t-1
        if (t < 0) {
          let time = (new Date().getTime())/1000
          console.log("time = " + time)
          tt.setStorage({
            key: 'gdtime',
            data: time
          })
          that.goAtherPageRedirect()
        } else {
          that.setData({
            time: t
          })
        }
      }, 1000)
  },

  /**清除定时器 */
  clearTimer() {
    if (this.requestTimer) {
      console.log("清除定时器")
      clearInterval(this.requestTimer)
    }
    this.requestTimer = ''
  },


  goAtherPageRedirect: function () {
    this.clearTimer()
    var url = "/pages/index/index"
    if (this.isFromShare) {
      url = url + "?share=true&uid=" + this.fromSubUid
    } else if (this.isFromSubMsg) {
      url = url + "?fmsg=true&fuid=" + this.fromSubUid
    } else if (this.goLiaota) {
      url = "/pages/liaota/liaota"
    }
    tt.redirectTo({
      url: url
    })
  }
})